package com.example.chatplatform.service.impl;

import com.example.chatplatform.mapper.GroupMapper;
import com.example.chatplatform.model.dto.CreateGroupDTO;
import com.example.chatplatform.model.dto.GroupAnmDTO;
import com.example.chatplatform.model.dto.GroupManagerDTO;
import com.example.chatplatform.model.vo.*;
import com.example.chatplatform.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class GroupServiceImpl implements GroupService {

    @Autowired
    public GroupMapper groupMapper;

    @Override
    public Integer groupAccount(CreateGroupDTO createGroupDTO) {
        return groupMapper.groupAccount(createGroupDTO);
    }

    @Override
    public Integer createGroup(CreateGroupDTO createGroupDTO, String fileUrl) {
        // 获取当前日期和时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 定义日期和时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化当前日期和时间为字符串
        String formattedDateTime = currentDateTime.format(formatter);
        createGroupDTO.setGroupTime(formattedDateTime);
        groupMapper.createGroup(createGroupDTO, fileUrl);
        if (createGroupDTO.getGroupId() == null) {
            return null;
        } else {
            groupMapper.addGroupUser(createGroupDTO);
            return createGroupDTO.getGroupId();
        }

    }

    @Override
    public List<GroupMemberVO> groupMember(Integer groupId) {
        return groupMapper.groupMember(groupId);
    }

    @Override
    public List<GroupAnmVO> groupAnm(Integer groupId) {
        return groupMapper.groupAnm(groupId);
    }

    @Override
    public boolean addGroupAnm(GroupAnmDTO groupAnmDTO) {
        // 获取当前日期和时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 定义日期和时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化当前日期和时间为字符串
        String formattedDateTime = currentDateTime.format(formatter);
        groupAnmDTO.setGroupAnmTime(formattedDateTime);
        return groupMapper.addGroupAnm(groupAnmDTO) > 0;
    }

    @Override
    public boolean setGroupManager(GroupManagerDTO groupManagerDTO) {
        // 获取当前日期和时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 定义日期和时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化当前日期和时间为字符串
        String formattedDateTime = currentDateTime.format(formatter);
        return groupMapper.setGroupManager(groupManagerDTO) > 0
                && groupMapper.addGroupInform(groupManagerDTO, "你已被设置为管理员", formattedDateTime) > 0;
    }

    @Override
    public boolean cancelGroupManager(GroupManagerDTO groupManagerDTO) {
        // 获取当前日期和时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 定义日期和时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化当前日期和时间为字符串
        String formattedDateTime = currentDateTime.format(formatter);
        return groupMapper.cancelGroupManager(groupManagerDTO) > 0
                && groupMapper.addGroupInform(groupManagerDTO, "你已被取消管理员", formattedDateTime) > 0;
    }

    @Override
    public boolean removeMember(GroupManagerDTO groupManagerDTO) {
        // 获取当前日期和时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 定义日期和时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化当前日期和时间为字符串
        String formattedDateTime = currentDateTime.format(formatter);
        groupNumber(groupManagerDTO.getGroupId(), -1);
        return groupMapper.removeMember(groupManagerDTO) > 0
                && groupMapper.addGroupInform(groupManagerDTO, "你已被请出群聊", formattedDateTime) > 0;
    }

    @Override
    public List<GroupInformVO> getGroupInform(Integer userId) {
        return groupMapper.getGroupInform(userId);
    }

    @Override
    public GroupDetailVO getGroupInfo(Integer userId, Integer groupId) {
        GroupDetailVO groupDetailVO = groupMapper.getGroupInfo(groupId);
        if (groupMapper.ifUserGroup(userId, groupId) == null) {
            groupDetailVO.setIfUserGroup(0);
        } else {
            groupDetailVO.setIfUserGroup(1);
            if (groupMapper.ifGroup(userId, groupId) == 3) {
                groupDetailVO.setIfGroup(1);
            } else {
                groupDetailVO.setIfGroup(0);
            }
        }
        return groupDetailVO;
    }

    @Override
    public boolean joinGroup(GroupManagerDTO groupManagerDTO) {
        if (groupMapper.joinGroup(groupManagerDTO) > 0) {
            groupNumber(groupManagerDTO.getGroupId(), 0);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean quitGroup(GroupManagerDTO groupManagerDTO) {
        if (groupMapper.quitGroup(groupManagerDTO) > 0) {
            groupNumber(groupManagerDTO.getGroupId(), 0);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean disbandGroup(GroupManagerDTO groupManagerDTO) {
        // 获取当前日期和时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 定义日期和时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化当前日期和时间为字符串
        String formattedDateTime = currentDateTime.format(formatter);
        List<Integer> groupUserId = groupMapper.groupUserId(groupManagerDTO);
        for (Integer item : groupUserId) {
            groupManagerDTO.setUserId(item);
            if (item != groupManagerDTO.getHandleId()) {
                groupMapper.addGroupInform(groupManagerDTO, "群聊已解散", formattedDateTime);
            }
            groupMapper.removeMember(groupManagerDTO);
        }
        groupMapper.removeMessage(groupManagerDTO);
        return groupMapper.disbandGroup(groupManagerDTO) > 0;
    }

    public void groupNumber(Integer groupId, Integer i) {
        int groupNumber = groupMapper.groupNumber(groupId);
        groupMapper.updateGroupNumber(groupId, groupNumber + i);
    }

}
